define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            console.log(location.search)
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'withdrawal/index' + location.search,
                    /*add_url: 'withdrawal/add',
                    edit_url: 'withdrawal/edit',*/
                    del_url: 'withdrawal/del',
                    multi_url: 'withdrawal/multi',
                    import_url: 'withdrawal/import',
                    table: 'withdrawal',
                }
            });

            var table = $("#table");
            table.on('load-success.bs.table', function (e, data) {
                if(data.source_data==1){
                    $('label[for="regionservice.name"]').parent().css('display','none');
                    table.bootstrapTable('hideColumn','regionservice.name'); //可以隐藏某一个列
                }
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type_data', title: __('Type_data'), searchList: {"0":__('Type_data 0'),"1":__('Type_data 1'),"2":__('Type_data 2')}, formatter: Table.api.formatter.normal},
                        {field: 'source_data', title: __('Source_data'), searchList: {"1":__('Source_data 1'),"2":__('Source_data 2'),"3":__('Source_data 3'),"4":__('Source_data 4')}, formatter: Table.api.formatter.normal},
                        //{field: 'user_id', title: __('User_id')},
                        {field: 'merchant.name', title: __('Merchant.name')},
                        {field: 'regionservice.name', title: __('Regionservice.name'),visible:false},
                        {field: 'dealer.name', title: __('Dealer.name'),visible:false},
                        {field: 'distributor.name', title: __('Distributor.name'),visible:false},
                        {field: 'regionservice.name', title: __('Regionservice.name'),visible: false},
                        {field: 'account', title: __('Account'), operate: false},
                        {field: 'account_image', title: __('Account_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'alipayAccount', title: __('AlipayAccount'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate:false},
                        {field: 'commission', title: __('Commission'), operate:false},
                        {field: 'actual_receipt', title: __('Actual_receipt'), operate:false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate_time', title: __('Operate_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'buttons', width: "90px", title: "审核", table: table, operate: false,
                            buttons: [
                                {
                                    name: 'operation',
                                    classname: 'btn btn-xs btn-info btn-refund',
                                    text: "驳回",
                                    icon: "",
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status !=0) {
                                            return false
                                        }
                                        return true;

                                    }
                                },
                            ],
                            events: {
                                'click .btn-refund': function (e, value, row) {
                                    var str="<div><p>申请提现金额："+row.price+"</p>"
                                        +"<p>手续费："+row.commission+"</p>"+"<p>实际打款："+row.actual_receipt+"</p>";
                                    str+='<p>拒绝理由:</p><textarea name="txt_remark" id="remark1" style="width:200px;height:100px;"></textarea></div>';
                                    Layer.prompt({
                                        title: "驳回",
                                        formType: 2,
                                        area: ['200px', '100px'], //自定义文本域宽高
                                        content:str,
                                        value: row.remark,
                                        btnAlign:'c',
                                        maxlength: 50,
                                        yes:function (index,layero) {
                                            var value=$("#remark1").val();
                                            Fast.api.ajax({
                                                url: "withdrawal/operation?id=" + row.id + '&status=2',
                                                data: {remark: value},
                                            }, function (data, ret) {
                                                Layer.closeAll();
                                                $(".btn-refresh").trigger("click");
                                            });
                                        }
                                    }, function (value, index, elem) {
                                        console.log(value);return
                                        Fast.api.ajax({
                                            url: "withdrawal/operation?id=" + row.id + '&status=2',
                                            data: {remark: value},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                        });
                                    });
                                    return false;
                                },
                            },
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons:[
                                {
                                    name: 'ok',
                                    title: '通过',
                                    text: '通过',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: function (row,index) {
                                        var str="<p>申请提现金额："+row.price+"</p>"
                                            +"<p>手续费："+row.commission+"</p>"+"<p>实际打款："+row.actual_receipt+"</p>"
                                        +"<p>是否确认打款？</p>";
                                        return str;
                                    },
                                    visible: function (row) {
                                        // console.log(row)
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status  !=0 ) {
                                            return false
                                        }
                                        return true;

                                    },
                                    url: function (row, column) {
                                        var url = 'withdrawal/operation?id=' + row.id + '&status=1';
                                        return url;
                                    },
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        layer.alert(ret.msg)
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },
                                {
                                    name: 'operation',
                                    classname: 'btn btn-xs btn-info btn-refund',
                                    text: "驳回",
                                    icon: "",
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status !=0) {
                                            return false
                                        }
                                        return true;

                                    }
                                },
                            ], events: {
                                'click .btn-refund': function (e, value, row) {
                                    var str="<div><p>申请提现金额："+row.price+"</p>"
                                        +"<p>手续费："+row.commission+"</p>"+"<p>实际打款："+row.actual_receipt+"</p>";
                                    str+='<p>拒绝理由:</p><textarea name="txt_remark" id="remark1" style="width:200px;height:100px;"></textarea></div>';
                                    Layer.prompt({
                                        title: "驳回",
                                        formType: 2,
                                        area: ['200px', '100px'], //自定义文本域宽高
                                        content:str,
                                        value: row.remark,
                                        btnAlign:'c',
                                        maxlength: 50,
                                        yes:function (index,layero) {
                                            var value=$("#remark1").val();
                                            Fast.api.ajax({
                                                url: "withdrawal/operation?id=" + row.id + '&status=2',
                                                data: {remark: value},
                                            }, function (data, ret) {
                                                Layer.closeAll();
                                                $(".btn-refresh").trigger("click");
                                            });
                                        }
                                    }, function (value, index, elem) {
                                        console.log(value);return
                                        Fast.api.ajax({
                                            url: "withdrawal/operation?id=" + row.id + '&status=2',
                                            data: {remark: value},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                        });
                                    });
                                    return false;
                                },
                                'click .btn-delone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var that = this;
                                    var top = $(that).offset().top - $(window).scrollTop();
                                    var left = $(that).offset().left - $(window).scrollLeft() - 260;
                                    if (top + 154 > $(window).height()) {
                                        top = top - 154;
                                    }
                                    if ($(window).width() < 480) {
                                        top = left = undefined;
                                    }
                                    Layer.confirm(
                                        __('Are you sure you want to delete this item?'),
                                        {
                                            icon: 3,
                                            title: __('Warning'),
                                            offset: [top, left],
                                            shadeClose: true,
                                            btn: [__('OK'), __('Cancel')]
                                        },
                                        function (index) {
                                            var table = $(that).closest('table');
                                            var options = table.bootstrapTable('getOptions');
                                            Table.api.multi("del", row[options.pk], table, that);
                                            Layer.close(index);
                                        }
                                    );
                                },
                                'click .btn-editone': function (e, value, row, index) {
                                    e.stopPropagation();
                                    e.preventDefault();
                                    var table = $(this).closest('table');
                                    var options = table.bootstrapTable('getOptions');
                                    var ids = row[options.pk];
                                    row = $.extend({}, row ? row : {}, {ids: ids});
                                    var url = options.extend.edit_url;
                                    Fast.api.open(Table.api.replaceurl(url, row, table), $(this).data("original-title") || $(this).attr("title") || __('Edit'), $(this).data() || {});
                                },
                            }, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var value=$(this).attr("data-value");
                console.log(value)
                if(value==1){
                    table.bootstrapTable('showColumn','merchant.name');
                    table.bootstrapTable('hideColumn','dealer.name');
                    table.bootstrapTable('hideColumn','distributor.name');
                    table.bootstrapTable('hideColumn','regionservice.name');
                }
                if(value==2){
                    table.bootstrapTable('hideColumn','merchant.name');
                    table.bootstrapTable('hideColumn','dealer.name');
                    table.bootstrapTable('hideColumn','distributor.name');
                    table.bootstrapTable('showColumn','regionservice.name');
                }
                if(value==3){
                    table.bootstrapTable('hideColumn','merchant.name');
                    table.bootstrapTable('hideColumn','regionservice.name');
                    table.bootstrapTable('hideColumn','distributor.name');
                    table.bootstrapTable('showColumn','dealer.name');
                }
                if(value==4){
                    table.bootstrapTable('hideColumn','merchant.name');
                    table.bootstrapTable('hideColumn','dealer.name');
                    table.bootstrapTable('showColumn','distributor.name');
                }
                return false;
            });
            table.bootstrapTable('hideColumn','buttons');
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

$('.btn-refund').on("click",function () {
    console.log(7);
})